package com.cx.mq.rabbitmq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author:chenxiaoyi
 * @Date:2020/8/8 11:33
 */

@Slf4j
@Configuration
@ConditionalOnBean(value = {ConfirmCallBackImpl.class,ReturnCallBackImpl.class})
public class RabbitMqConfig /* implements RabbitListenerConfigurer */{

    /**
     * 获取当前配置RabbitMq连接工厂 注入此时配置好的RabbitMQ连接 eg:host:port 等
     */
    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Autowired
    private ReturnCallBackImpl returnCallBack;

    @Autowired
    private ConfirmCallBackImpl confirmCallBack;

    /***
     * 配置RabbitTemplate
     */
    @Bean
    public RabbitTemplate rabbitTemplate(){
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setConfirmCallback(confirmCallBack);
        template.setReturnCallback(returnCallBack);
        return template;
    }
    @Bean
    MessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

//    @Override
//    public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
//        registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
//    }
//
//    @Bean
//    MessageHandlerMethodFactory messageHandlerMethodFactory() {
//        DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
//        messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
//        return messageHandlerMethodFactory;
//    }
//
//    @Bean
//    public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
//        return new MappingJackson2MessageConverter();
//    }
}
